package com.webbucks;



import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;





/**
 *
 * @author Karl Viring
 */
public class ProductBean {
    String name;
    List<Ingredient> ingredients = new ArrayList<Ingredient>();
    int price;
    
    
            String url = "jdbc:mysql://localhost:8889/";
        String dbName = "webbucks";
        String driver = "com.mysql.jdbc.Driver";
        String user = "root"; 
        String pw = "root";
    
    public ProductBean() { 
    } 
    
    public ProductBean(String n) { 

            
        List<ProductBean> products = new ArrayList<ProductBean>();        

        String Query="Select ingredient from products where name=?";
        
        String Query2="Select price from price where name=?";        

        ResultSet rs=null;
        ResultSet rs2=null;

        PreparedStatement pstmt=null;
        PreparedStatement pstmt2=null;

        Connection con=null;
        
        
        //set name
        this.setName(n);
        
        try {
            Class.forName(driver).newInstance();
            con=DriverManager.getConnection(url+dbName,user,pw);
            
            pstmt=con.prepareStatement(Query);
            pstmt2=con.prepareStatement(Query2);
    
            //set ingridient
            pstmt.setString(1, n);
            pstmt2.setString(1, n);
            
            rs=pstmt.executeQuery();                
            while (rs.next()) {
                this.addIngredient(rs.getString("ingredient"));
            }

            //set price                
            rs2=pstmt2.executeQuery();
            rs2.next();
            this.setPrice(rs2.getInt("price"));
            
        } catch( Exception e ){
            System.out.println("Something went wrong" + e.toString() );
        } finally {
            if (rs != null) try { rs.close(); } catch (SQLException ignore) {}
            if (rs2 != null) try { rs2.close(); } catch (SQLException ignore) {}
            if (pstmt != null) try { pstmt.close(); } catch (SQLException ignore) {}
            if (pstmt2 != null) try { pstmt2.close(); } catch (SQLException ignore) {}
            if (con != null) try { con.close(); } catch (SQLException ignore) {}
        }
            
    
    } 
    
/* Setters
 */
    public void setName(String n) {
        name = n;
    }

    public void setPrice(int p) {
        price = p;
    }
    
    
    
    public void addIngredient(String s) {
        Ingredient i;
        Iterator<Ingredient> iter = ingredients.listIterator();

        
        
        int currentSupply;
        if (iter.hasNext()) {
            while (iter.hasNext()) {
                i = iter.next();
                boolean supplyExists = false;
                try {
                    Class.forName(driver).newInstance();
                    Connection con=DriverManager.getConnection(url+dbName,user,pw);
                    String Query = "Select supply from ingredients where name=\"" +s + "\"";
                    System.out.println(Query);
                    Statement stmt = con.createStatement();
                    ResultSet srs = stmt.executeQuery(Query);
                    srs.next();
                    currentSupply = srs.getInt("supply");
                    supplyExists = true;
                    System.out.println("Done!? currentsupply=" + currentSupply);
                } catch( Exception e ){
                    System.out.println("Something went wrong" + e.toString() );
                    currentSupply = 0;
                } 
                if(supplyExists){
                    if (i.getName().equals(s)) {
                        i.addIngredient(currentSupply);
                    } else if (!iter.hasNext()) {   //last
                        System.out.println("Försöker lägga in på rätt ställe iaf...");
                        iter=null;
                        ingredients.add(new Ingredient(s, currentSupply));
                        break;
                    }
                }
            }
        } else { //ingredients == empty
            ingredients.add(new Ingredient(s, 0));
        }
    }
        
     
    public void removeIngredient(String s) {

        Iterator<Ingredient> iter = ingredients.listIterator();

        while (iter.hasNext()) {
            if (iter.next().getName().equals(s)) {
                iter.remove();
            } 
        }           
    }


/* Getters
 */
    public String getName() {
        return name;    
    }
    
    public List<Ingredient> getIngredients() {
        return ingredients;    
    }

    public int getPrice() {
        return price;    
    }


   public static List<ProductBean> list() throws SQLException, IOException {
        String url = "jdbc:mysql://localhost:8889/";
        String dbName = "webbucks";
        String driver = "com.mysql.jdbc.Driver";
        String user = "root"; 
        String pw = "root";
            
        List<ProductBean> products = new ArrayList<ProductBean>();        

        String Query;
        String Query2;
        String Query3;
        
        ResultSet rs=null;
        ResultSet rs2=null;
        ResultSet rs3=null;
        
        Statement stmt=null;
        Statement stmt2=null;
        Statement stmt3=null;

        Connection con=null;
        
        try {
            Class.forName(driver).newInstance();
            con=DriverManager.getConnection(url+dbName,user,pw);

            Query = "Select DISTINCT name from products";

            stmt = con.createStatement();
            stmt2 = con.createStatement();
            stmt3 = con.createStatement();

            rs = stmt.executeQuery(Query);         

            System.out.println("Print 1");
           
            while (rs.next()) {

                ProductBean product = new ProductBean();
                
                //set name
                product.setName(rs.getString("name"));
                
                //set ingridient
                Query2 = "Select ingredient from products where name='" + product.getName()+"'";
                rs2=stmt2.executeQuery(Query2);                
                while (rs2.next()) {
                    System.out.println("Trying to add ingredient = "+rs2.getString("ingredient"));    
                    product.addIngredient(rs2.getString("ingredient"));
                    System.out.println("Print 3");

                }

                //set price                
                Query3 = "Select price from price where name='" +product.getName()+"'";
                rs3=stmt3.executeQuery(Query3);
                rs3.next();
                product.setPrice(rs3.getInt("price"));
                // add to products list
                products.add(product);
                System.out.println("Print 4");
                

            }
            
        } catch( Exception e ){
            System.out.println("Something went wrong" + e.toString() );
        } finally {
            if (rs != null) try { rs.close(); } catch (SQLException ignore) {}
            if (rs2 != null) try { rs2.close(); } catch (SQLException ignore) {}
            if (rs3 != null) try { rs3.close(); } catch (SQLException ignore) {}
            if (stmt != null) try { stmt.close(); } catch (SQLException ignore) {}
            if (stmt2 != null) try { stmt2.close(); } catch (SQLException ignore) {}
            if (stmt3 != null) try { stmt3.close(); } catch (SQLException ignore) {}
            if (con != null) try { con.close(); } catch (SQLException ignore) {}
        }
        return products;
    }    
    
}


